<?php

/**
 * Spectacle form base class.
 *
 * @method Spectacle getObject() Returns the current form's model object
 *
 * @package    tfe
 * @subpackage form
 * @author     3emeIG
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseSpectacleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'titre'                 => new sfWidgetFormInputText(),
      'auteur'                => new sfWidgetFormInputText(),
      'lieu'                  => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Lieu'), 'add_empty' => false)),
      'date_de_debut'         => new sfWidgetFormDate(),
      'date_de_fin'           => new sfWidgetFormDate(),
      'heure_de_debut'        => new sfWidgetFormTime(),
      'duree'                 => new sfWidgetFormTime(),
      'type_de_public'        => new sfWidgetFormChoice(array('choices' => array('enfants' => 'enfants', 'ados' => 'ados', 'adultes' => 'adultes', 'ados-adultes' => 'ados-adultes', 'tous publics' => 'tous publics'))),
      'age_mini'              => new sfWidgetFormInputText(),
      'commentaire'           => new sfWidgetFormTextarea(),
      'type'                  => new sfWidgetFormInputText(),
      'deleted_at'            => new sfWidgetFormDateTime(),
      'created_at'            => new sfWidgetFormDateTime(),
      'updated_at'            => new sfWidgetFormDateTime(),
      'abonnements_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Abonnement')),
      'metteur_en_scene_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste')),
      'acteurs_list'          => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste')),
      'intervenants_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'titre'                 => new sfValidatorString(array('max_length' => 100)),
      'auteur'                => new sfValidatorString(array('max_length' => 255)),
      'lieu'                  => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Lieu'))),
      'date_de_debut'         => new sfValidatorDate(array('required' => false)),
      'date_de_fin'           => new sfValidatorDate(array('required' => false)),
      'heure_de_debut'        => new sfValidatorTime(array('required' => false)),
      'duree'                 => new sfValidatorTime(array('required' => false)),
      'type_de_public'        => new sfValidatorChoice(array('choices' => array(0 => 'enfants', 1 => 'ados', 2 => 'adultes', 3 => 'ados-adultes', 4 => 'tous publics'), 'required' => false)),
      'age_mini'              => new sfValidatorInteger(array('required' => false)),
      'commentaire'           => new sfValidatorString(array('required' => false)),
      'type'                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'deleted_at'            => new sfValidatorDateTime(array('required' => false)),
      'created_at'            => new sfValidatorDateTime(),
      'updated_at'            => new sfValidatorDateTime(),
      'abonnements_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Abonnement', 'required' => false)),
      'metteur_en_scene_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'required' => false)),
      'acteurs_list'          => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'required' => false)),
      'intervenants_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('spectacle[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Spectacle';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['abonnements_list']))
    {
      $this->setDefault('abonnements_list', $this->object->Abonnements->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['metteur_en_scene_list']))
    {
      $this->setDefault('metteur_en_scene_list', $this->object->MetteurEnScene->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['acteurs_list']))
    {
      $this->setDefault('acteurs_list', $this->object->Acteurs->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['intervenants_list']))
    {
      $this->setDefault('intervenants_list', $this->object->Intervenants->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveAbonnementsList($con);
    $this->saveMetteurEnSceneList($con);
    $this->saveActeursList($con);
    $this->saveIntervenantsList($con);

    parent::doSave($con);
  }

  public function saveAbonnementsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['abonnements_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Abonnements->getPrimaryKeys();
    $values = $this->getValue('abonnements_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Abonnements', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Abonnements', array_values($link));
    }
  }

  public function saveMetteurEnSceneList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['metteur_en_scene_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MetteurEnScene->getPrimaryKeys();
    $values = $this->getValue('metteur_en_scene_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MetteurEnScene', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MetteurEnScene', array_values($link));
    }
  }

  public function saveActeursList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['acteurs_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Acteurs->getPrimaryKeys();
    $values = $this->getValue('acteurs_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Acteurs', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Acteurs', array_values($link));
    }
  }

  public function saveIntervenantsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['intervenants_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Intervenants->getPrimaryKeys();
    $values = $this->getValue('intervenants_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Intervenants', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Intervenants', array_values($link));
    }
  }

}
